function task_new_show()
{
    if ($('form_task').currentStyle.display == 'block') {
		if (task_edit_id != -1) {
			task_edit_cancel();
		} else {
			task_new_cancel();
			return;
		}
    }
    task_form_show('form_task');
	var iday = $('task_new_day');
    var itime = $('task_new_time');
	if (!iday.value.length) {
		iday.value = getDay();
	}
	if (!itime.value.length) {
		itime.value = '0:00';
	}
    $('task_new_name').focus();
}
function task_new_cancel()
{
    task_form_hide();
}
function task_new()
{
    var iday = $('task_new_day');
    var itime = $('task_new_time');
    var iname = $('task_new_name');
    var day = iday.value.trim();
    var time = itime.value.trim();
    var name = iname.value.trim();
    if (day) day = formatDay(day);
    else day = getDay();
    if (!time) time = 0;
    if (!name) {
        alert(lang.task_name_empty);
        return false;
    }
    time = getTime(time);
    if (-1 == time) {
        alert(lang.invalid_time);
        return false;
    }
    var new_id = ++tasks.seq_id;
    tasks.rows[new_id] = {
        'id': new_id,
        'day': escapeTags(day),
        'time': Number(time),
        'name': escapeTags(name)
    };
    task_save();
    iday.value = '';
    itime.value = '';
    iname.value = '';
    task_list();
    task_form_hide();
    if (-1 == tasks.active && 0 == time) {
        task_start(new_id);
    }
}
function task_list()
{
    var html = '<table class="ls" cellspacing="1" cellpadding="0"><tr><th width="10%">'+lang.day+'</th><th>'+lang.task+'</th><th width="10%">'+lang.time+'</th><th width="10%" align="center">'+lang.action+'</th></tr>';
    var rows = tasks_rows_latest();
	if (!rows.length) {
		$('tasks_div').innerHTML = '<div class="cont">'+lang.no_tasks_found+'.</div>';
		return;
	}
    for (var i = 0; i < rows.length; i++)
    {
        var task = rows[i];
        var act = '';
        if (tasks.active == task.id) {
            act = html_stop(task.id);
        } else {
            act = html_start(task.id);
        }
        html += ('<tr><td align="center" nowrap>'+formatDay(task.day)+'</td><td id="task_name_'+task.id+'"><span onclick="task_edit_show('+task.id+')" style="cursor: pointer;">'+nl2br(task.name)+'</span></td><td id="task_time_'+task.id+'" align="center">'+formatTime(task.time)+'</td><td align="center" id="task_action_'+task.id+'" class="stop">'+act+'</td></tr>').replace(/<td/g, '<td style="background: #eee;"');
    }
    html += '</table>';
    $('tasks_div').innerHTML = html;
}
var task_edit_id = -1;
function task_edit_show(id)
{
    if (task_edit_id == id) {
        task_edit_cancel();
        return;
    }
    task_form_show('form_task');
    task_edit_id = id;
    $('task_new_button').style.display = 'none';
    $('task_edit_span').style.display = '';
    var iday = $('task_new_day');
    var itime = $('task_new_time');
    var iname = $('task_new_name');
    var task = tasks.rows[id];
    iday.value = task.day;
    itime.value = formatTime(task.time);
    iname.value = task.name;
    $('task_new_name').focus();
}
function task_edit_cancel()
{
    if (task_edit_id == -1) {
        return;
    }
    task_edit_id = -1;
    $('task_new_button').style.display = '';
    $('task_edit_span').style.display = 'none';
    var iday = $('task_new_day');
    var itime = $('task_new_time');
    var iname = $('task_new_name');
    iday.value = '';
    itime.value = '';
    iname.value = '';
    task_form_hide();
}
function task_edit()
{
    var iday = $('task_new_day');
    var itime = $('task_new_time');
    var iname = $('task_new_name');
    var day = iday.value.trim();
    var time = itime.value.trim();
    var name = iname.value.trim();
    if (day) day = formatDay(day);
    else day = getDay();
    if (!time) time = 0;
    if (!name) {
        alert(lang.task_name_empty);
        return false;
    }
    time = getTime(time);
    if (-1 == time) {
        alert(lang.invalid_time);
        return false;
    }
    var id = task_edit_id;
    tasks.rows[id].day = escapeTags(day);
    tasks.rows[id].time = Number(time);
    tasks.rows[id].name = escapeTags(name);
    task_save();
    task_list();
    task_edit_cancel();
    task_form_hide();
}
function task_delete()
{
    if (confirm(lang.task_delete_confirm)) {
        task_remove(task_edit_id);
        task_save();
        task_list();
        task_edit_cancel();
        task_form_hide();
    }
}
function task_remove(id)
{
    if (tasks.active == id) {
        tasks.active = -1;
    }
    var ret = {};
    for (var i in tasks.rows) {
        if (id == i) continue;
        ret[i] = tasks.rows[i];
    }
    tasks.rows = ret;
}
function task_start(id)
{
    if (tasks.active != -1) {
        var active = tasks.rows[tasks.active];
        $('task_action_'+active.id).innerHTML = html_start(active.id);
        $('task_time_'+active.id).innerHTML = formatTime(active.time);
    }
    tasks.active = id;
    __micro = time_start();
    $('task_action_'+id).innerHTML = html_stop(id);
    task_interval_sec();
    task_save();
}
function task_stop(id)
{
    tasks.active = -1;
    __micro = false;
    $('task_action_'+id).innerHTML = html_start(id);
    $('task_time_'+id).innerHTML = formatTime(tasks.rows[id].time);
    task_save();
}
function html_start(id)
{
    return '<a href="#" onclick="task_start('+id+')" class="start">start</a>';
}
function html_stop(id)
{
    return '<input type="button" class="submit stop" value=" stop " onclick="task_stop('+id+')">';
}
function total_time()
{
    var ret = 0;
    for (var id in tasks.rows) {
        ret += tasks.rows[id].time;
    }
    return ret;
}
function total_update()
{
    var html = '';
    var totalTime = total_time();
    var rows = tasks_rows();
    var rate = curr_project.rate;
    if (rows.length) {
        html += '&nbsp;&nbsp;<i>|</i>&nbsp;&nbsp;';
        html += lang.total_time+': <b>'+formatTime(totalTime)+'</b>';
    }
    if (rows.length && rate) {
        html += '&nbsp;&nbsp;<i>|</i>&nbsp;&nbsp;';
        html += lang.total_amount+': <b>'+time_round((totalTime/60)*rate)+'</b> '+curr_currency();
    }
    $('total_span').innerHTML = html;
}
function task_form_show(id)
{
    task_edit_cancel();
    $('form_task').style.display = 'block';
}
function task_form_hide()
{
    $('form_task').style.display = 'none';
}
function task_refresh()
{
    task_new_cancel();
    task_edit_cancel();
    select_project_render();
    total_update();
    task_list();
}